Shader "GameEffect/AllEffect01_MaskRA_ST"
{
    Properties
    {
        _Tex01("主贴图", 2D) = "white" {}
        [HDR]_Tex01Color("贴图颜色", Color) = (1,1,1,1)
        _Intensity("整体强度", Float) = 1
        _TexSpeedU("主贴图流动U", Float) = 0
        _TexSpeedV("主贴图流动V", Float) = 0
        _Tex01Rot("主贴图旋转", Range(0, 1)) = 0
        _Texture_R_A("主贴图透明度通道选择 (0=R, 1=A)", Range(0,1)) = 0

        _Tex01Mask("遮罩贴图", 2D) = "white" {}
        _Mask01SpeedU("遮罩流动U", Float) = 0
        _Mask01SpeedV("遮罩流动V", Float) = 0
        _MaskRot("遮罩旋转", Range(0, 1)) = 0
        _Mask_R_A("遮罩通道选择 (0=R, 1=A)", Range(0,1)) = 0

        [HDR]_EmissionColor("自发光颜色", Color) = (0,0,0,0)
        _EmissionIntensity("自发光强度", Float) = 0
    }

    SubShader
    {
        Tags { "Queue"="Transparent" "RenderType"="Transparent" }
        LOD 100
        Blend SrcAlpha OneMinusSrcAlpha
        Cull Off
        ZWrite Off

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _Tex01;
            float4 _Tex01_ST;
            float4 _Tex01Color;
            float _Intensity;
            float _TexSpeedU;
            float _TexSpeedV;
            float _Tex01Rot;
            float _Texture_R_A;

            sampler2D _Tex01Mask;
            float4 _Tex01Mask_ST;
            float _Mask01SpeedU;
            float _Mask01SpeedV;
            float _MaskRot;
            float _Mask_R_A;

            float4 _EmissionColor;
            float _EmissionIntensity;

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                float4 color : COLOR;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                float4 color : COLOR;
            };

            float2 RotateUV(float2 uv, float angle)
            {
                float2 center = float2(0.5, 0.5);
                float rad = angle * 6.2831853; // 2π
                float s = sin(rad);
                float c = cos(rad);
                uv -= center;
                float2 rotated = float2(
                    uv.x * c - uv.y * s,
                    uv.x * s + uv.y * c
                );
                return rotated + center;
            }

            v2f vert(appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                o.color = v.color;
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                // 主贴图处理
                float2 uvMain = TRANSFORM_TEX(i.uv, _Tex01);
                uvMain += _Time.y * float2(_TexSpeedU, _TexSpeedV);
                uvMain = RotateUV(uvMain, _Tex01Rot);
                fixed4 mainCol = tex2D(_Tex01, uvMain);

                // 遮罩贴图处理
                float2 uvMask = TRANSFORM_TEX(i.uv, _Tex01Mask);
                uvMask += _Time.y * float2(_Mask01SpeedU, _Mask01SpeedV);
                uvMask = RotateUV(uvMask, _MaskRot);
                fixed4 maskCol = tex2D(_Tex01Mask, uvMask);
                float maskVal = lerp(maskCol.r, maskCol.a, _Mask_R_A);

                // 颜色与透明度
                float3 rgb = mainCol.rgb * maskVal * i.color.rgb * _Tex01Color.rgb;
                float mainAlpha = lerp(mainCol.r, mainCol.a, _Texture_R_A);
                float alpha = mainAlpha * maskVal * i.color.a * _Tex01Color.a;

                // 自发光
                float3 emission = _EmissionColor.rgb * _EmissionIntensity;

                return fixed4(rgb + emission, alpha) * _Intensity;
            }
            ENDCG
        }
    }
}
